package com.wugenqiang.project.inventory.controller;

import com.wugenqiang.constant.ParamConstant;
import com.wugenqiang.project.inventory.pojo.Outofwarehouse;
import com.wugenqiang.project.inventory.pojo.Storage;
import com.wugenqiang.project.inventory.pojo.Warehouse;
import com.wugenqiang.project.inventory.pojo.vo.OutofwarehouseVo;
import com.wugenqiang.project.inventory.pojo.vo.WarehouseVo;
import com.wugenqiang.project.inventory.service.IOutofwarehouseService;
import com.wugenqiang.project.inventory.service.IStorageService;
import com.wugenqiang.project.system.controller.BaseController;
import com.wugenqiang.utils.date.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname OutofwarehouseController
 * @Description TODO
 * @Date 2019/3/8 16:21
 * @Created by WuGenQiang//fighting!!
 */
@Controller
@RequestMapping("/lfd/admin/inventory/outofwarehouse")
public class OutofwarehouseController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OutofwarehouseController.class);

    @Resource
    private IStorageService storageService;

    @Resource
    private IOutofwarehouseService outofwarehouseService;

    @RequestMapping(value = "")
    public String ShowOutofwarehouseGoodsList(Model model) {

        List<OutofwarehouseVo> outofwarehouseVos = new ArrayList<>();
        List<Outofwarehouse> outofwarehouses = null;
        outofwarehouses = outofwarehouseService.queryAllGoods();
        for(int i=0;i<outofwarehouses.size();i++){
            OutofwarehouseVo outofwarehouseVo = new OutofwarehouseVo();
            outofwarehouseVo.setId(outofwarehouses.get(i).getId());
            outofwarehouseVo.setIdentifier(outofwarehouses.get(i).getIdentifier());
            Storage storage = storageService.queryGoodsByIdentifier(outofwarehouses.get(i).getIdentifier());
            outofwarehouseVo.setName(storage.getName());
            outofwarehouseVo.setModel(storage.getModel());
            outofwarehouseVo.setAmount(outofwarehouses.get(i).getAmount());
            outofwarehouseVo.setUnit(storage.getUnit());
            outofwarehouseVo.setConsignee(outofwarehouses.get(i).getConsignee());
            outofwarehouseVo.setTime(DateUtils.formatDateByUnixTime(outofwarehouses.get(i).getTime(),"yyyy-MM-dd"));
            outofwarehouseVo.setRemark(outofwarehouses.get(i).getRemark());
            outofwarehouseVos.add(outofwarehouseVo);
        }
        model.addAttribute("outofwarehouseVos", outofwarehouseVos);
        return "inventory/outofwarehouse_list";
    }

    // 跳转到出库页面
    @RequestMapping(value = "/toAddOutofwarehouse")
    public String toAddOutofwarehouse(HttpServletRequest request, Model model) {
        Integer id = Integer.parseInt(request.getParameter("id").trim());
        LOGGER.info(id+"");
        Storage storage = storageService.queryGoodsById(id);
        OutofwarehouseVo outofwarehouseVo = new OutofwarehouseVo();
        outofwarehouseVo.setId(id);
        outofwarehouseVo.setIdentifier(storage.getIdentifier());
        outofwarehouseVo.setName(storage.getName());
        outofwarehouseVo.setAmount(0);
        outofwarehouseVo.setModel(storage.getModel());
        outofwarehouseVo.setUnit(storage.getUnit());
        outofwarehouseVo.setRemark("");
        outofwarehouseVo.setConsignee("");
        outofwarehouseVo.setTime("");
        LOGGER.info(outofwarehouseVo.toString());
        model.addAttribute("outofwarehouseVo", outofwarehouseVo);
        return "inventory/outofwarehouse_add";
    }

    // 更新操作
    @RequestMapping(value = "/addOutofwarehouse", method = RequestMethod.POST)
    public String addOutofwarehouse(Model model, HttpServletRequest request) {

        Integer id = Integer.parseInt(request.getParameter("id").trim());
        Storage storage = storageService.queryGoodsById(id);
        Outofwarehouse outofwarehouse = new Outofwarehouse();
        outofwarehouse.setIdentifier(storage.getIdentifier());
        String time = request.getParameter("time");
        if(time==null||time==""){
            outofwarehouse.setTime(DateUtils.getCurrentUnixTime());
        }else{
            outofwarehouse.setTime((int) (DateUtils.dateFormat(time, "yyyy-MM-dd").getTime() / 1000L));
        }
        outofwarehouse.setAmount(Integer.valueOf(request.getParameter("amount")));
        outofwarehouse.setRemark(request.getParameter("remark"));
        outofwarehouse.setConsignee(request.getParameter("consignee"));
        if(storage.getAmount()!=0){
            storage.setAmount(storage.getAmount()-outofwarehouse.getAmount());
        }else{
            storage.setBalance(storage.getBalance()-outofwarehouse.getAmount());
        }
        storageService.updateGoodsInfo(storage);
        String result = outofwarehouseService.addGoodsInfo(outofwarehouse);
        if (result.equals(ParamConstant.INSERT_SUCCESS) == true) {

            List<Storage> storages = null;
            storages = storageService.queryAllGoods();
            model.addAttribute("storages", storages);
            return "inventory/storage_list";
        } else {
            return "inventory/outofwarehouse_add";

        }
    }

}
